// pages/index/index.js


// 链接服务器
wx.cloud.init({
  env: 't1-4gjkpt1t1e37bab9', //填上你的云开发环境id
  traceUser: true,
})
const db = wx.cloud.database();
const appUser = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    notice: "",
    announcement: "",
    group: [{
        img: "/asstess/lr.png",
        title: "老人健康",
        to: 1
      },
      {
        img: "/asstess/xxl.png",
        title: "老人心理",
        to: 2
      },
      {
        img: "/asstess/TT.png",
        title: "预约服务",
        to: 3
      },
      {
        img: "/asstess/yy.png",
        title: "告警中心",
        to: 6
      },
      {
        img: "/asstess/lradmin.png",
        title: "老人管理",
        to: 4
      },
      {
        img: "/asstess/yly.png",
        title: "养老院",
        to: 5
      },
     
      {
        img: "/asstess/device.png",
        title: "资讯中心",
        to: 7
      },
      {
        img: "/asstess/activity.png",
        title: "活动中心",
        to: 8
      }



    ],
    title: "",
    text: "",
    time: "",
    source: "",
    img: [],
    arr: [],
    movies: [{
        url: '/asstess/1.jpg'
      },

      {
        url: '/asstess/4.jpg'
      },
      {
        url: '/asstess/6.jpg'
      },

      {
        url: '/asstess/5.jpg'
      }
    ],
    news_list: [{
      title: "",
      img: "",
      time: "",
      url: ""
    }, {
      title: "",
      img: "",
      time: "",
      url: ""
    }, {
      title: "",
      img: "",
      time: "",
      url: ""
    }, {
      title: "",
      img: "",
      time: "",
      url: ""
    }, {
      title: "",
      img: "",
      time: "",
      url: ""
    }]
  },
  toDetail(e) {
    var {
      index,
      id
    } = e.currentTarget.dataset
    console.log(e)


    switch (index) {
      case 0:
        wx.navigateTo({
          url: appUser.globalData.userOpenid ? '../servicedetile/servicedetile?index=' + id : '../login/login',
        })
        break
      case 1:
        wx.navigateTo({
          url: appUser.globalData.userOpenid ? '../servicedetile/servicedetile?index=' + id : '../login/login',
        })
        break

    }
  },
  
  toShop() {
    wx.navigateTo({
      url: '../shop/shop',
    })
  },
  toAdvisory() {
    wx.navigateTo({
      url: '../advisory/advisory',
    })
  },
  navigator(e) {
    
    const index = e.currentTarget.dataset.index
    console.log(index)
    if (index == 1) {
      wx.navigateTo({
        url: appUser.globalData.userOpenid ? '../oldman/oldman' : '../login/login',
      })
    }
    if (index == 2) {
      wx.navigateTo({
        url: appUser.globalData.userOpenid ? '../oldmentality/oldmentality' : '../login/login',
      })
    }
    if (index == 3) {
      wx.navigateTo({
        url: appUser.globalData.userOpenid ? '../lifeservice/lifeservice' : '../login/login',
      })
    }
    if (index == 4) {
      wx.navigateTo({
        url: appUser.globalData.userOpenid ? '../oldadmin/oldadmin' : '../login/login',
      })
    }
    if (index == 7) {
      wx.navigateTo({
        url: appUser.globalData.userOpenid ? '../advisory/advisory' : '../login/login',
      })
    }
    if (index == 8) {
      wx.navigateTo({
        url: appUser.globalData.userOpenid ? '../activity/activity' : '../login/login',
      })
    }
    if (index == 5) {
      wx.switchTab({
        url: appUser.globalData.userOpenid ? '../nursing/nursing' : '../login/login',
      })
     
    }

  },
  news_list_tap(url) {
    url = url.currentTarget.dataset.url;
    //console.log(id);
    wx.navigateTo({
      url: '../news/news?url=' + url,
    })
  },
  onLoad() {
    db.collection("notice").get({
      success: res => {
        this.setData({
          notice: res.data[0].notice
        })
      }
    })
    this.getBanner()
    this.getRlist()
    wx.request({
      url: 'https://www.yanglaocn.com/zhengcefagui/',
      method: 'GET',
      success: res => {
        // 获取新闻链接
        var ylHttpps = res.data.match(/https:\/\/m\.yanglaocn.com\/shtml\/.*title/g)
        //数组去重
        ylHttpps = Array.from(new Set(ylHttpps))
        ////console.log(ylHttpps)
        var lj;
        // var ylzx = [];
        //获取前五个新闻的链接
        for (var i = 0; i < 5; i++) {
          lj = ylHttpps[i].split('\"')[0]
          this.data.news_list[i].url = lj
        }

        //获取前五个新闻的图片
        var newsImg = res.data.match(/.*\n+lazy-load/g);
        newsImg.length = 5;
        for (var i = 0; i < 5; i++)
          this.data.news_list[i].img = newsImg[i].split("\"")[1];

        //获取前五个新闻的标题
        var newsTitle;
        newsTitle = res.data.match(/title.*<\/span><\/a>/g)
        for (var i = 0; i < 5; i++) {
          this.data.news_list[i].title = newsTitle[i].split('"><span>')[0].split("=\"")[1]
        }

        //获取前五个新闻时间
        var newsTime;
        newsTime = res.data.match(/upload\/img\/.*\/.*\//g);
        var ye;
        for (var i = 0; i < 5; i++) {
          ye = newsTime[i * 2].split('upload/img/')[1].split('/')
          this.data.news_list[i].time = ye[0] + '_' + ye[1]
        }
        this.setData({
          news_list: this.data.news_list
        })
      }
    })

  },

  getRlist() {
    wx.cloud.callFunction({
      name: "service",
      data: {
        type: "selectR"
      }
    }).then(res => {
      console.log(res.result.data)
      const obj = res.result.data.map(item => ({
        cover: item.img,
        title: item.title,
        id: item._id
      }))
      console.log(obj)
      this.data.list = obj
      this.setData({
        list: this.data.list
      })
    })

  },
  getBanner() {
    wx.cloud.callFunction({
      name: "banner",
      data: {
        type: "select"
      }
    }).then(res => {
      console.log(res.result.data)
      this.setData({
        movies: res.result.data
      })
    })
  }
})